package co.fitstart.admin.web.controller.finance;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.principal.PrincipalType;

@Controller
@RequestMapping("/staff-codes")
@MenuSetting("menu-finance")
public class StaffCodeController {
    
    @Autowired
    private PrincipalService principalService;
    
    private Logger logger = LoggerFactory.getLogger(StaffCodeController.class);
    
    /**
     * 
     * @param page
     * @return
     */
    @RequestMapping(name = "", method = RequestMethod.GET)
    @MenuSetting("submenu-staff-management")
    public ModelAndView list(@RequestParam(name = "code", required = false, defaultValue = "false") boolean code, 
                             Page page) {
        ModelAndView modelAndView = new ModelAndView("finance/department/staff-list");
        
        List<Principal> principals = principalService.list(code, PrincipalType.SITE, page);
        
        modelAndView.addObject("principals", principals);
        
        modelAndView.addObject("code", code);
        modelAndView.addObject("page", page);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param siteId
     * @return
     */
    @RequestMapping(value = "/{principalId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("principalId") Long principalId) {
        ModelAndView modelAndView = new ModelAndView("finance/department/staff-code-edit");
        
        Principal principal = principalService.load(Principal.class, principalId);
        
        modelAndView.addObject("principal", principal);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param site
     * @return
     */
    @RequestMapping(value = "/{principalId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("principalId") Principal principal) {
        
        Result result = new Result();
        
        try {
            
            principalService.update(principal);
            
            result.success().message("修改员工编号成功");
            
        } catch (Exception e) {
            
            result.fail().message("修改员工编号失败");
            
            logger.error("#onEdit() error ", e);
            
        }
        
        return JsonUtils.toJsonString(result);
    }

}
